import vue from 'rollup-plugin-vue';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import path from 'path';
import copy from 'rollup-plugin-copy';
import alias from '@rollup/plugin-alias';
import json from '@rollup/plugin-json';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const entries = {
  index: 'src/index.js',
  HelloPlugin: 'src/entries/HelloPlugin.js',
  HelloWorld: 'src/entries/HelloWorld.js',
  VxeGridPro: 'src/entries/VxeGridPro.js',
  VxeTablePro: 'src/entries/VxeTablePro.js',
  VxeModalPro: 'src/entries/VxeModalPro.js', // 新增插件入口
  SearchPro: 'src/entries/SearchPro.js', // SearchPro 插件入口
};

export default [
  // JS & CSS
  {
    input: entries,
    output: [
      {
        dir: 'dist',
        format: 'es',
        entryFileNames: '[name]/index.js',
        chunkFileNames: 'chunks/[name]-[hash].js',
        assetFileNames: '[name][extname]',
        exports: 'named',
        sourcemap: false, // 关闭所有 source map
        preserveModules: true,
        preserveModulesRoot: 'src',
      },
    ],
    plugins: [
      alias({
        entries: [
          { find: '@', replacement: path.resolve(__dirname, 'src') }
        ]
      }),
      vue({
        target: 'browser',
        css: false,
        exposeFilename: false,
      }),
      postcss({
        extract: 'CI.Web.Plugins.Commom.css',
        minimize: true,
        sourceMap: false, // 关闭 source map
      }),
      resolve({
        preferBuiltins: false,
        extensions: ['.js', '.vue', '.json']
      }),
      commonjs(),
      terser(),
      copy({
        targets: [
          {
            src: 'src/components/*/README.md',
            dest: 'dist',
            rename: (name, extension, fullPath) => {
              // fullPath: src/components/插件名/README.md
              const match = fullPath.match(/src[\\/\\]components[\\/\\]([^\\/\\]+)[\\/\\]README\.md$/);
              if (match) {
                return `${match[1]}/README.md`;
              }
              return `${name}${extension}`;
            }
          }
        ],
        flatten: true
      }),
    ],
    //external: ['vue'],
    external: ['vue', 'element-plus', 'vxe-table','axios','dayjs'],
  },
  // SearchPro 单独构建配置 - 不使用 preserveModules，将所有依赖打包到一个文件
  {
    input: 'src/entries/SearchPro.js',
    output: {
      file: 'dist/SearchPro/SearchPro.bundle.js',
      format: 'es',
      exports: 'named',
      sourcemap: false,
    },
    plugins: [
      alias({
        entries: [
          { find: '@', replacement: path.resolve(__dirname, 'src') }
        ]
      }),
      json(),
      vue({
        target: 'browser',
        css: false,
        exposeFilename: false,
      }),
      postcss({
        extract: false, // 不提取 CSS，内联到 JS 中
        minimize: true,
        sourceMap: false,
      }),
      resolve({
        preferBuiltins: false,
        browser: true,
        extensions: ['.js', '.vue', '.json']
      }),
      commonjs(),
      terser(),
    ],
    external: ['vue', 'vxe-table', 'dayjs', 'element-plus', 'axios'],
  },
];
